package me.hys.service.chat.impl;

import java.sql.Timestamp;

import me.hys.base.ApiResult;
import me.hys.base.PageQuery;
import me.hys.chat.comm.ChatConstants;
import me.hys.chat.comm.ChatException;
import me.hys.cons.ApiStatus;
import me.hys.entity.doctor.AnswerEntity;
import me.hys.kits.MD5Kit;
import me.hys.service.chat.IChatManager;
import me.hys.service.doctor.IAnswerManager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;

/**
 * Author: @author HYS . <br>
 * Date: 2015-10-6 上午1:09:56 . <br>
 * Function:
 */
@Service
class ChatManagerImpl extends BaseChatManagerImpl implements IChatManager {
	@Autowired
	private IAnswerManager answerManager;

	/*
	 * (non-Javadoc)
	 * 
	 * @see me.hys.service.chat.IChatManager#delete()
	 */
	@Override
	public ApiResult delete(String userid) {
		ApiResult apiResult = new ApiResult(ApiStatus.SUCCESS);
		try {
			deleteIMUserByUserName(ChatConstants.NAME_PREFIX + userid);
		} catch (ChatException e) {
			apiResult.setResult(ApiStatus.FAILED);
			apiResult.setMsg(e.getMessage());
			e.printStackTrace();
		}
		return apiResult;
	}

	/**
	 * @Title : findMsgPage
	 * @Description :
	 */
	@Override
	public PageQuery<AnswerEntity> findMsgPage(PageQuery<AnswerEntity> page) {
		return answerManager.findPage(page);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see me.hys.service.chat.IChatManager#login(java.lang.String,
	 * java.lang.String)
	 */
	@Override
	public ApiResult login(String userid, String mobile) {
		ApiResult apiResult = new ApiResult(ApiStatus.SUCCESS);
		ObjectNode json2 = JsonNodeFactory.instance.objectNode();
		json2.put("password", MD5Kit.encrypt(mobile));
		try {
			imUserLogin(ChatConstants.NAME_PREFIX + userid,
					json2.get("password").asText());
		} catch (ChatException e) {
			apiResult.setResult(ApiStatus.FAILED);
			apiResult.setMsg(e.getMessage());
			e.printStackTrace();
		}
		return apiResult;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see me.hys.service.chat.IChatManager#modifyNickname(java.lang.String,
	 * java.lang.String)
	 */
	@Override
	public ApiResult modifyNickname(String userid, String nickname) {
		ApiResult apiResult = new ApiResult(ApiStatus.SUCCESS);
		ObjectNode json2 = JsonNodeFactory.instance.objectNode();
		json2.put("nickname", nickname);
		try {
			// 改密码
			modifyIMUserNicknameWithAdminToken(ChatConstants.NAME_PREFIX
					+ userid, json2);
		} catch (ChatException e) {
			apiResult.setResult(ApiStatus.FAILED);
			apiResult.setMsg(e.getMessage());
			e.printStackTrace();
		}
		return apiResult;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see me.hys.service.chat.IChatManager#modifyPassword(java.lang.String,
	 * java.lang.String)
	 */
	@Override
	public ApiResult modifyPassword(String userid, String mobile) {
		ApiResult apiResult = new ApiResult(ApiStatus.SUCCESS);
		ObjectNode json2 = JsonNodeFactory.instance.objectNode();
		json2.put("newpassword", MD5Kit.encrypt(mobile));
		try {
			// 改密码
			modifyIMUserPasswordWithAdminToken(ChatConstants.NAME_PREFIX
					+ userid, json2);
			// 重新登录
			imUserLogin(ChatConstants.NAME_PREFIX + userid,
					json2.get("newpassword").asText());
		} catch (ChatException e) {
			apiResult.setResult(ApiStatus.FAILED);
			apiResult.setMsg(e.getMessage());
			e.printStackTrace();
		}
		return apiResult;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see me.hys.service.chat.IChatManager#register(java.lang.String,
	 * java.lang.String)
	 */
	@Override
	public ApiResult register(String userid, String mobile, String nickname) {
		ApiResult apiResult = new ApiResult(ApiStatus.SUCCESS);
		ObjectNode datanode = JsonNodeFactory.instance.objectNode();
		datanode.put("username", ChatConstants.NAME_PREFIX + userid);
		datanode.put("password", MD5Kit.encrypt(mobile));
		datanode.put("nickname", nickname);
		try {
			createNewIMUserSingle(datanode);
		} catch (ChatException e) {
			apiResult.setResult(ApiStatus.FAILED);
			apiResult.setMsg(e.getMessage());
			e.printStackTrace();
		}
		return apiResult;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see me.hys.service.chat.IChatManager#sendMsg(java.lang.String,
	 * java.lang.String)
	 */
	@Override
	public ApiResult sendMsg(String msg, String from, String target) {
		ApiResult apiResult = new ApiResult(ApiStatus.SUCCESS);
		// ObjectNode ext = factory.objectNode();
		// ext.put("diagnoseId", diagnoseId);
		ArrayNode targetusers = factory.arrayNode();
		targetusers.add(ChatConstants.NAME_PREFIX + target);
		ObjectNode txtmsg = factory.objectNode();
		txtmsg.put("msg", msg);
		txtmsg.put("type", "txt");
		try {
			sendMessages(targetusers, txtmsg, ChatConstants.NAME_PREFIX + from,
					null);
			// 保存聊天记录到本地服务器
			AnswerEntity entity = new AnswerEntity();
			entity.setMsgFrom(from);
			entity.setMsgTo(target);
			entity.setMsg(msg);
			entity.setCreateTime(new Timestamp(System.currentTimeMillis()));
			answerManager.save(entity);
		} catch (ChatException e) {
			apiResult.setResult(ApiStatus.FAILED);
			apiResult.setMsg(e.getMessage());
			e.printStackTrace();
		}
		return apiResult;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see me.hys.service.chat.IChatManager#sendOpenMsg()
	 */
	@Override
	public void sendOpenMsg(String action, String from, String[] target) {
		// 给用户发一条透传消息
		ObjectNode cmdmsg = factory.objectNode();
		cmdmsg.put("action", action);
		cmdmsg.put("type", "cmd");
		ArrayNode targetUser = factory.arrayNode();
		for (String u : target) {
			targetUser.add(u);
		}
		try {
			sendMessages(targetUser, cmdmsg, from, null);
		} catch (ChatException e) {
			e.printStackTrace();
		}
	}
}
